﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

namespace hlog {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：PLDictCache                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/7/4 14:38:50                        
   *└────────────────────────────────────────────────┘
   **/
    public class GuidangCache {
        private static object obj = new object();

        private static Dictionary<string ,List<BaseMongoLog>> cacheData = new Dictionary<string ,List<BaseMongoLog>>();

        public static String systemId ;
        public static String systemName ;
        static GuidangCache() {
            systemName = Process.GetCurrentProcess().ProcessName;
            try {
                    string HostName = Dns.GetHostName(); //得到主机名
                    IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                    for( int i = 0;i < IpEntry.AddressList.Length;i++ ) {
                        //从IP地址列表中筛选出IPv4类型的IP地址
                        //AddressFamily.InterNetwork表示此IP为IPv4,
                        //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                        IPAddress ip = IpEntry.AddressList[i];
                        if( ip.AddressFamily == AddressFamily.InterNetwork) {
                            systemId = systemName+(int)(ip.Address>>24);
                        break;
                        }
                    }
                } catch {
                    
                }
        }
       
        public static void addMessage(string s) {
            add("message_log" ,new MessageLog(s));
        }


        static void add(string name,BaseMongoLog log) {
            lock( obj ) {
                if( cacheData.ContainsKey(name) ) {
                    cacheData[name].Add(log);
                } else {
                    cacheData.Add(name ,new List<BaseMongoLog> { log });
                }
                if( cacheData[name].Count > 100000 ) {
                    cacheData[name].RemoveAt(0);
                }
            }
        }

        public static void addLog(BaseMongoLog log) {
            string name = log.GetType().Name;
            add(name ,log);
        }
       

        public static Dictionary<string ,List<BaseMongoLog>> getAllMongoData() {
            lock( obj ) {
                Dictionary<string ,List<BaseMongoLog>> tmp = cacheData;
                cacheData = new Dictionary<string ,List<BaseMongoLog>>();
                return tmp;
            }

        }
    


    }
}
